# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:login1_0.py
@Time:2024/11/5 15:45

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
import os

def daojishi(n):
    # 设置倒计时时间（秒）
    countdown_time = n * 60  # 5分钟
    while countdown_time > 0:
        minutes, seconds = divmod(countdown_time, 60)
        # 使用 \r 回到行首，end='' 防止换行
        print(f"\r倒计时: {minutes:02}:{seconds:02}", end='')
        sleep(1)
        countdown_time -= 1
    print("\n倒计时结束！")

def read_credentials():
    if os.path.exists('a.txt'):
        with open('a.txt', 'r') as file:
            username = file.readline().strip()
            password = file.readline().strip()
            return username, password
    return None, None

def write_credentials(username, password):
    with open('a.txt', 'w') as file:
        file.write(username + '\n')
        file.write(password + '\n')

# 指定 ChromeDriver 的路径
driver_path = 'd:/code_zgq/ganbu/chromedriver.exe'
service = Service(executable_path=driver_path)

# 创建 WebDriver 实例
driver = webdriver.Chrome(service=service)
driver.implicitly_wait(5)
driver.maximize_window()

# 打开网页
driver.get('https://www.sxgbxx.gov.cn/login')

# 读取或请求用户输入用户名和密码
username, password = read_credentials()
if not username or not password:
    username = input("请输入用户名: ")
    password = input("请输入密码: ")
    write_credentials(username, password)

# 找到用户名输入框并输入用户名
driver.find_element(By.ID, 'userEmail').send_keys(username)
# 找到密码输入框并输入密码
driver.find_element(By.ID, 'userPassword').send_keys(password)
# 提示用户输入验证码
input("按回车继续...")
